package GUI;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DropMode;
import javax.swing.JTree;
import javax.swing.tree.TreePath;

import Context.Constant;
import Data.User;
import Data.UserCategory;
import Data.UserGroup;
import Personal.PersonalConfiguration;

public class ContactTree {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7606315521974036363L;

	// The tree component.
	private JTree contactTree;
	// The root node of the tree.
	private CategoryNode root;
	/**
	 * Create a tree with the given category type.
	 * 
	 * @param categoryType
	 *            The type of the category
	 */
	public ContactTree() {
		loadTree();
	}

	/**
	 * Load the nodes of the tree according to the data.
	 * 
	 * @param categoryType
	 * @return
	 */
	public JTree loadTree() {
		UserCategory userCategory = PersonalConfiguration
				.getPersonalConfiguration().getUserCategory();
		buildTree(userCategory);
		return contactTree;
	}
	/**
	 * Get the tree component.
	 * @return
	 */
	public JTree getTree() {
		return contactTree;
	}
	/**
	 * A new user login. The user will set the state to online.
	 * If the user does not exist in the category, it will append
	 * into the first group.
	 * @param newUser
	 */
	void loginUser(User newUser) {
		UserCategory userCategory = root.getUserCategory();
		for (UserGroup userGroup : userCategory.getAllgroups()) {
			for (User currentUser : userGroup.getGroupUsers()) {
				if(newUser.getIp().equals(currentUser.getIp()))
				{
					currentUser.setUserState(Constant.ONLINE);
					userGroup.sortUsers();
					return;
				}
			}
		}
		UserGroup defaultGroup = userCategory.getAllgroups().get(0);
		newUser.setUserState(Constant.ONLINE);
		defaultGroup.addUser(newUser);
		
	}
	/**
	 * A user log out. It will set the state of the user to off line.
	 * @param newUser
	 */
	void logoutUser(User newUser) {
		UserCategory userCategory = root.getUserCategory();
		for (UserGroup userGroup : userCategory.getAllgroups()) {
			for (User currentUser : userGroup.getGroupUsers()) {
				if(newUser.getIp().equals(currentUser.getIp()))
				{
					currentUser.setUserState(Constant.OFFLINE);
					userGroup.sortUsers();
					return;
				}
			}
		}
	}
	/**
	 * Build the tree component with the given user category
	 * Each group in the category is mapped to a group node.
	 * Each user in the group is mapped to a user node.
	 * @param userCategory
	 */
	private void buildTree(UserCategory userCategory) {
		root = new CategoryNode(userCategory);
		for (UserGroup userGroup : userCategory.getAllgroups()) {
			GroupNode newGroupNode = new GroupNode(userGroup);
			root.add(newGroupNode);
			for (User user : userGroup.getGroupUsers()) {
				UserNode newUserNode = new UserNode(user);
				newGroupNode.add(newUserNode);
			}
		}
		contactTree = new JTree(root);
		contactTree.setRootVisible(false);
		contactTree.setDragEnabled(true);
		contactTree.setDropMode(DropMode.ON_OR_INSERT);
		contactTree.addMouseListener(new MouseListener() {

			// @Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				if (e.getButton() == MouseEvent.BUTTON3) {
					IMenuPopuper selectedNode = (IMenuPopuper) contactTree
							.getLastSelectedPathComponent();
					if (selectedNode == null)
						return;
					else {
						selectedNode.popUpMenu(contactTree, e.getX(), e.getY());
					}
				}
			}

			// @Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			// @Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			// @Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				if (e.getModifiers() == MouseEvent.META_MASK) {

					TreePath treePath = contactTree.getPathForLocation(
							e.getX(), e.getY());
					if (treePath != null)
						contactTree.setSelectionPath(treePath);
					else
						contactTree.setSelectionPath(null);
					return;

				}
			}

			// @Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});

	}
}
